<template>
  <div
    class="login-container"
    :style="{ backgroundImage: 'url(' + bannerImage + ')' }"
  >
  <div class="header-view">
     <div class="main-cont header clearfix">
      <div class="logo" >
        <!-- <router-link to="/"><img src="../../assets/home/logo.png" alt="" /></router-link> -->
          <img src="../../assets/home/logo.png" alt="Logo" />
      
      </div>
    </div>
  </div>
     
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">绘王活动赛事评审系统</h3>
      </div>

      <el-form-item prop="account">
        <!-- <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>-->
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="登录账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <!-- <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>-->
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="登录密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button
        class="btn"
        :loading="loading"
        type="primary"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <!-- <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span>password: any</span>
      </div>-->
    </el-form>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import { login } from "@/api/user";
import { getBanner } from "@/api/banner";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的帐号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位数字"));
      } else {
        callback();
      }
    };
    return {
      bannerImage: require("../../assets/login-bg.jpg"),
      loginForm: {
        account: "admin00001",
        password: "123456"
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getLoginBanner();
  },
  methods: {
    getLoginBanner() {
      getBanner().then(res => {
        this.bannerImage = this.$format_img(res.banner.url);
      });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    async handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true;

          try {
            await this.$store.dispatch("user/login", this.loginForm);
            await this.$store.dispatch("user/getInfo");
            const userLevel = this.$store.getters.userLevel;
            const activityId = this.$store.getters.activity_id;
             const pathUrl=userLevel+'/'+ activityId
            // console.log(userLevel, activityId);
            switch (userLevel) {
              case "level1":
                setTimeout(() => {
                  this.$router.push({ path: "/home/" + pathUrl });
                }, 500);
                break;
              case "level2":
                setTimeout(() => {
                  this.$router.push({ path: "/home/" + pathUrl });
                }, 500);
                break;
              case "level3":
                setTimeout(() => {
                  this.$router.push({ path: "/home/" + pathUrl });
                }, 500);
                break;
              case "/":
                setTimeout(() => {
                  this.$router.push("/");
                }, 500);
                break;
              default:
                this.$message.error("未知的用户级别，请联系管理员");
                break;
            }

            this.loading = false;
          } catch (error) {
            this.$message.error("登录失败，请检查您的账号和密码");
            this.loading = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>

.header-view {
  position: fixed;
  width: 100%;
  background: #000000;
  /* opacity: 0.8; */
  /* height: 75px; */
  top: 0;
  left: 0;
  z-index: 9999;
}
.header {
  /* padding: 0 4%; */
  height: 70px;
  position: relative;
}
.header-view .main-cont {
  position: relative;
}
.logo {
  width: 190px;
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
}
.logo img {
cursor: pointer;
  width: 100%;
  vertical-align: middle;
}

</style>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;

    input {
      font-size: 18px;
      // background: transparent;
      border-top: none;
      border-left: none;
      border-right: none;
      border-bottom: 1px solid #000;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        // -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background-size: cover;
  background-repeat: no-repeat;

  .login-form {
    border-radius: 14px;
    text-align: center;
    background: #fff;
    height: 480px;
    width: 540px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 80px 70px;
    box-sizing: border-box;
  }
  .btn {
    margin-top: 40px;
    width: 100%;
    height: 60px;
    font-size: 24px;
    background: #00bfd6;
  }
  .tips {
    font-size: 14px;
    color: #000;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-weight: bold;
      font-size: 36px;
      color: #000000;
      margin: 0px auto 40px auto;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
